<script setup lang="ts">
import * as echarts from "echarts";
import {onMounted} from "vue";


onMounted(() => {
  init()
})

function init() {
  const chart = echarts.init(document.getElementById('mainChart'));

  // 模拟数据 - 8个模具的数据
  const categories = ['模具1', '模具2', '模具3', '模具4',];
  const wireData = [45, 52, 48, 55,];
  const speedData = [12.5, 13.2, 12.8, 13.5,];
  const diameterData = [0.25, 0.28, 0.26, 0.30,];

  // 图表配置
  const option = {
    grid: {
      top: 30,
      left: 10,
      right: 20,
      bottom: 0,
      containLabel: true
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      },
      backgroundColor: 'rgba(10, 20, 40, 0.9)',
      borderColor: '#00f7ff',
      borderWidth: 2,
      textStyle: {
        color: '#ffffff'
      },
      // formatter: function (params) {
      //   let result = '<div style="font-weight:bold;margin-bottom:5px;">'
      //       + params[0].name + '</div>';
      //   params.forEach(item => {
      //     let unit = item.seriesName === '线径(mm)' ? 'mm' : 'm/s';
      //     result += `<div style="display:flex;align-items:center;margin:3px 0;">
      //                               <span style="display:inline-block;width:10px;height:10px;background:${item.color};margin-right:5px;border-radius:2px;"></span>
      //                               ${item.seriesName}: <span style="font-weight:bold;margin-left:5px;">${item.value} ${unit}</span>
      //                             </div>`;
      //   });
      //   return result;
      // }
    },
    legend: {
      data: ['拉丝速度(m/s)', '收线速度(m/s)', '线径(mm)'],
      textStyle: {
        color: 'rgba(255, 255, 255, 0.8)',
      },
      // right: 20,
      // top: 10
    },
    xAxis: {
      type: 'category',
      data: categories,
      axisLine: {
        lineStyle: {
          color: 'rgba(0, 247, 255, 0.5)'
        }
      },
      axisLabel: {
        color: 'rgba(0, 247, 255, 0.8)',
        rotate: 30,
        fontSize: 2
      },
      axisTick: {
        alignWithLabel: true,
        lineStyle: {
          color: 'rgba(0, 247, 255, 0.2)'
        }
      },
      splitLine: {
        show: true,
        lineStyle: {
          color: 'rgba(0, 247, 255, 0.1)'
        }
      }
    },
    yAxis: [
      {
        type: 'value',
        name: '速度 (m/s)',
        nameTextStyle: {
          color: 'rgba(0, 247, 255, 0.8)',
          // padding: [0, 0, 0, 40]
        },
        min: 10,
        max: 100,
        axisLine: {
          lineStyle: {
            color: 'rgba(0, 247, 255, 0.5)'
          }
        },
        axisLabel: {
          color: 'rgba(0, 247, 255, 0.8)',
          formatter: '{value}'
        },
        axisTick: {
          lineStyle: {
            color: 'rgba(0, 247, 255, 0.2)'
          }
        },
        splitLine: {
          lineStyle: {
            color: 'rgba(0, 247, 255, 0.1)'
          }
        }
      },
      {
        type: 'value',
        name: '线径 (mm)',
        nameTextStyle: {
          color: 'rgba(0, 247, 255, 0.8)',
          padding: [0, 40, 0, 0]
        },
        min: 0.2,
        max: 0.35,
        axisLine: {
          lineStyle: {
            color: 'rgba(255, 0, 170, 0.5)'
          }
        },
        axisLabel: {
          color:  'rgb(0,255,136)',
          formatter: '{value}'
        },
        axisTick: {
          lineStyle: {
            color: 'rgba(255, 0, 170, 0.2)'
          }
        },
        splitLine: {
          show: false
        }
      }
    ],
    series: [
      {
        name: '拉丝速度(m/s)',
        type: 'bar',
        barWidth: 15,
        data: wireData,
        itemStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {offset: 0, color: '#00a2ff'},
            {offset: 1, color: '#00f7ff'}
          ]),
          borderRadius: [4, 4, 0, 0]
        },
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 247, 255, 0.5)'
          }
        },
        label: {
          show: true,
          position: 'top',
          color: '#ffffff',
          formatter: '{c} m/s'
        }
      },
      {
        name: '收线速度(m/s)',
        type: 'bar',
        barWidth: 15,
        data: speedData,
        itemStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {offset: 0, color: '#00ff88'},
            {offset: 1, color: '#00aaff'}
          ]),
          borderRadius: [4, 4, 0, 0]
        },
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 255, 136, 0.5)'
          }
        },
        label: {
          show: true,
          position: 'top',
          color: '#ffffff',
          formatter: '{c} m/s'
        }
      },
      {
        name: '线径(mm)',
        type: 'line',
        yAxisIndex: 1,
        smooth: true,
        data: diameterData,
        symbol: 'circle',
        symbolSize: 8,
        lineStyle: {
          width: 3,
          color: '#00ff88'
        },
        itemStyle: {
          color: '#00ff88',
          borderColor: '#ffffff',
          borderWidth: 2
        },
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(255, 0, 170, 0.5)'
          }
        },
        label: {
          show: true,
          position: 'top',
          color: '#ffffff',
          backgroundColor: 'rgb(0,142,75)',
          padding: [2, 6],
          borderRadius: 4,
          formatter: '{c} mm'
        }
      }
    ]
  };

  chart.setOption(option);

  // 模拟数据更新
  function updateData() {
    // 更新巡检数据
    // document.getElementById('todayInspection').textContent =
    //     Math.floor(12 + Math.random() * 3);
    // document.getElementById('monthInspection').textContent =
    //     Math.floor(156 + Math.random() * 10);

    // 更新图表数据
    const newWireData = wireData.map(value => {
      return Math.round(Math.max(40, Math.min(60, value + (Math.random() - 0.5) * 2)));
    });

    const newSpeedData = speedData.map(value => {
      return Math.round(Math.max(11, Math.min(14, value + (Math.random() - 0.5) * 0.3)));
    });

    const newDiameterData = diameterData.map(value => {
      return Math.max(0.22, Math.min(0.32, value + (Math.random() - 0.5) * 0.02)).toFixed(3);
    });

    chart.setOption({
      series: [
        {data: newWireData},
        {data: newSpeedData},
        {data: newDiameterData}
      ]
    });

    // 每3秒更新一次数据
    setTimeout(updateData, 1000);
  }

  // 启动数据更新
  updateData();

  // 窗口大小调整时重新调整图表大小
  window.addEventListener('resize', function () {
    chart.resize();
  });
}
</script>

<template>
  <div>
    <div id="right2" class="monitor-panel">
      <div class="corner corner-tl"></div>
      <div class="corner corner-tr"></div>
      <div class="corner corner-bl"></div>
      <div class="corner corner-br"></div>

      <div>
        <div class="panel-title">微拉机生产监控</div>
        <div class="panel-subtitle">拉丝速度 / 收线速度 / 线径数据实时监测</div>
      </div>

      <div class="chart-container" id="mainChart"></div>

<!--      <div class="stats-container">-->
<!--        <div class="stat-card">-->
<!--          <div class="stat-title">当日巡检次数</div>-->
<!--          <div class="stat-value" id="todayInspection">12</div>-->
<!--          <div class="stat-unit">次</div>-->
<!--        </div>-->
<!--        <div class="stat-card">-->
<!--          <div class="stat-title">本月巡检次数</div>-->
<!--          <div class="stat-value" id="monthInspection">156</div>-->
<!--          <div class="stat-unit">次</div>-->
<!--        </div>-->
<!--        <div class="stat-card">-->
<!--          <div class="stat-title">总巡检次数</div>-->
<!--          <div class="stat-value" id="totalInspection">2,458</div>-->
<!--          <div class="stat-unit">次</div>-->
<!--        </div>-->
<!--      </div>-->
    </div>
  </div>
</template>

<style scoped>

/* 主面板容器 */
.monitor-panel {
  width: 21%;
  height: 27%;
  background: rgba(10, 20, 40, 0.7);
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 0 30px rgba(0, 247, 255, 0.3);
  border: 1px solid rgba(0, 247, 255, 0.2);
  position: absolute;
  overflow: hidden;
  backdrop-filter: blur(5px);
  display: grid;
  grid-template-rows: auto 1fr auto;
  gap: 15px;
}

/* 科技感边框效果 */
.monitor-panel::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  border: 1px solid transparent;
  border-image: linear-gradient(
      to right,
      rgba(0, 247, 255, 0),
      rgba(0, 247, 255, 0.8),
      rgba(0, 247, 255, 0)
  ) 1 1;
  pointer-events: none;
}

/* 网格背景 */
.monitor-panel::after {
  content: '';
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  background: linear-gradient(90deg, rgba(0, 247, 255, 0.1) 1px, transparent 1px) 0 0 / 20px 20px,
  linear-gradient(rgba(0, 247, 255, 0.1) 1px, transparent 1px) 0 0 / 20px 20px;
  pointer-events: none;
  opacity: 0.3;
  z-index: 0;
}

/* 四角装饰 */
.corner {
  position: absolute;
  width: 15px;
  height: 15px;
  border-style: solid;
  border-color: #00f7ff;
  pointer-events: none;
}

.corner-tl {
  top: 0;
  left: 0;
  border-width: 2px 0 0 2px;
}

.corner-tr {
  top: 0;
  right: 0;
  border-width: 2px 2px 0 0;
}

.corner-bl {
  bottom: 0;
  left: 0;
  border-width: 0 0 2px 2px;
}

.corner-br {
  bottom: 0;
  right: 0;
  border-width: 0 2px 2px 0;
}

/* 面板标题 */
.panel-title {
  font-size: 22px;
  margin: 0;
  text-transform: uppercase;
  letter-spacing: 2px;
  color: #00f7ff;
  text-shadow: 0 0 10px rgba(0, 247, 255, 0.7);
  position: relative;
  z-index: 1;
}

/* 面板副标题 */
.panel-subtitle {
  font-size: 14px;
  margin: 5px 0 0 0;
  color: rgba(0, 247, 255, 0.8);
  position: relative;
  z-index: 1;
}

/* 图表容器 */
.chart-container {
  width: 100%;
  height: 100%;
  position: relative;
  z-index: 1;
}

/* 统计信息容器 */
.stats-container {
  position: absolute;
  top: 0;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 15px;
  margin-top: 10px;
}

/* 统计卡片 */
.stat-card {
  background: rgba(0, 247, 255, 0.1);
  border-radius: 6px;
  padding: 5px;
  border: 1px solid rgba(0, 247, 255, 0.2);
  position: relative;
  overflow: hidden;
  transition: all 0.3s ease;
}

.stat-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 5px 15px rgba(0, 247, 255, 0.2);
}

.stat-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 2px;
  background: linear-gradient(90deg, transparent, #00f7ff, transparent);
  animation: flow 3s linear infinite;
}

@keyframes flow {
  0% {
    background-position: -100% 0;
  }
  100% {
    background-position: 100% 0;
  }
}

.stat-title {
  font-size: 14px;
  color: rgba(0, 247, 255, 0.8);
  margin-bottom: 10px;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.stat-value {
  font-size: 28px;
  font-weight: bold;
  color: #ffffff;
  text-shadow: 0 0 10px rgba(255, 255, 255, 0.7);
  margin: 5px 0;
}

.stat-unit {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.6);
}

/* 图例容器 */
.legend-container {
  position: absolute;
  top: 15px;
  right: 15px;
  display: flex;
  gap: 15px;
  z-index: 2;
}

.legend-item {
  display: flex;
  align-items: center;
  font-size: 12px;
  color: rgba(255, 255, 255, 0.8);
}

.legend-color {
  width: 12px;
  height: 12px;
  margin-right: 6px;
  border-radius: 2px;
}

.monitor-panel:hover {
  transform: translateY(-5px) scale(1.02);
  box-shadow: 0 0 30px rgba(0, 247, 255, 0.5);
}
</style>
